package com.zjgsu.nftplatform.controller;

import com.zjgsu.nftplatform.pojo.Result;
import com.zjgsu.nftplatform.pojo.ResultCode;
import com.zjgsu.nftplatform.service.NftService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

/**
 * @author ahy231
 * @date 2022/3/24 21:01
 * @description
 */
@Api(tags = "Nft合约")
@RestController
@RequestMapping("/nft")
public class NftController {
    private final NftService nftService;

    public NftController(NftService nftService) {
        this.nftService = nftService;
    }

    @ApiOperation("getIdByFileHash")
    @GetMapping("/idByFileHash")
    public Result getIdByFileHash(@ApiParam("文件哈希") @RequestParam String fileHash) {
        try {
            return new Result(ResultCode.SUCCESS, nftService.getIdByFileHash(fileHash));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("getNft")
    @GetMapping("/get")
    public Result getNft(@ApiParam("nft id") @RequestParam String id) {
        try {
            return new Result(ResultCode.SUCCESS, nftService.getNft(id));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("createNft")
    @PostMapping("/create")
    public Result createNft(@ApiParam("文件哈希") @RequestParam String fileHash,
                            @ApiParam("用户地址") @RequestParam String address,
                            @ApiParam("密码") @RequestParam String password) {
        try {
            return new Result(ResultCode.SUCCESS, nftService.createNft(fileHash, address, password));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }
    
    @ApiOperation("adapt")
    @PostMapping("/adapt")
    public Result adapt(@ApiParam("父母id") @RequestParam String parents,
                        @ApiParam("文件哈希") @RequestParam String fileHash,
                        @ApiParam("用户地址") @RequestParam String address,
                        @ApiParam("密码") @RequestParam String password) {
        try {
            return new Result(ResultCode.SUCCESS, nftService.adapt(parents, fileHash, address, password));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }
}
